/*
 * Options.java
 *
 * Created on 8 de Fevereiro de 2009, 05:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trigger.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * @author vinicius
 */
public class Options {
    
    
    private static final File CONFIGURATION_FILE = new File(System.getProperty("user.dir"),"UpdateCFG.ini");
    
    private static Options configuration;
    
    private Options()
    {
        if (CONFIGURATION_FILE.exists() || CONFIGURATION_FILE.canRead())
            loadFile(false);
        else
            loadFile(true);
    }
    
    public synchronized static Options getInstance()
    {
        if (configuration==null)
        {
            configuration=new Options();
        }
        return configuration;
    }
    
    private String server;
    private String proxyHost;
    private int proxyPort;
    private String proxyUsername;
    private String proxyPassword;
    private int timeoutTime;
    private boolean autoupdate;
    
    private final void loadFile(boolean dflt)
    {
        InputStream in=null;

        try 
        {  
            Properties p = new Properties();
            in = dflt ? getClass().getResourceAsStream("/trigger/config/Default.ini") : new FileInputStream(CONFIGURATION_FILE);
            p.load(in);
            
            this.setServer(p.getProperty("connection.server",""));
            this.setProxyHost(p.getProperty("connection.proxyserver",""));
            try{
                this.setProxyPort(Integer.parseInt(p.getProperty("connection.proxyport","80")));
            }catch(NumberFormatException nex){
                this.setProxyPort(80);
            }
            this.setProxyUsername(p.getProperty("connection.proxyuser",""));
            this.setProxyPassword(p.getProperty("connection.proxpass",""));
            try{
                this.setTimeoutTime(Integer.parseInt(p.getProperty("connection.timeout","30")));
            }catch(NumberFormatException nex){
                this.setTimeoutTime(30);
            }
            this.setAutoupdate(p.getProperty("autoupdate","true").equals("true"));
            
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public final void saveConfiguration()
    {
        try {
            Properties properties = new Properties();
            
            properties.setProperty("connection.server", configuration.getServer());
            properties.setProperty("connection.proxyserver", configuration.getProxyHost());
            properties.setProperty("connection.proxyport",String.valueOf(configuration.getProxyPort()));
            properties.setProperty("connection.proxyuser", configuration.getProxyUsername());
            properties.setProperty("connection.proxpass", configuration.getProxyPassword());
            properties.setProperty("connection.timeout",String.valueOf(configuration.getTimeoutTime()));
            properties.setProperty("autoupdate", String.valueOf(configuration.isAutoupdate()));

            properties.store(new FileOutputStream(CONFIGURATION_FILE), "Configuration File of Update System");

        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public int getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getProxyUsername() {
        return proxyUsername;
    }

    public void setProxyUsername(String proxyUsername) {
        this.proxyUsername = proxyUsername;
    }

    public String getProxyPassword() {
        return proxyPassword;
    }

    public void setProxyPassword(String proxyPassword) {
        this.proxyPassword = proxyPassword;
    }

    public int getTimeoutTime() {
        return timeoutTime;
    }

    public void setTimeoutTime(int timeoutTime) {
        this.timeoutTime = timeoutTime;
    }

    public boolean isAutoupdate() {
        return autoupdate;
    }

    public void setAutoupdate(boolean autoupdate) {
        this.autoupdate = autoupdate;
    }
    
}
